﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;

namespace LAura
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Member

        private DispatcherTimer m_timer;

        private static readonly string Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

        private Options m_optionsWindow;

        private static NotifyIcon _trayIcon;
        private static ContextMenu _trayMenu;
        private static MenuItem _closeMenuItem;
        private static MenuItem _optionsMenuItem;

        private String m_lyncActualStatus;
        private String m_lyncLastStatus;

        private int m_red;
        private int m_green;
        private int m_blue;
        private int m_errorLync = 1;

        private Lync m_localLync;

        #endregion



        #region Properties
        #endregion



        #region Constructor

        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion


        #region Methods

        private void WindowSourceInitialized(object sender, EventArgs e)
        {
            Visibility = Visibility.Collapsed;
            AddIcon();

            m_timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
            m_timer.Tick += TimerTick;
            m_timer.Start();

            WinAPI.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        }

        public void AddIcon()
        {
            _trayMenu = new ContextMenu();

            _closeMenuItem = new MenuItem {Text = Properties.Resources.Close};
            _closeMenuItem.Click += CloseMenuItemClick;

            _optionsMenuItem = new MenuItem {Text = Properties.Resources.Options};
            _optionsMenuItem.Click += OptionsMenuItemClick;

            _trayMenu.MenuItems.Add(_optionsMenuItem);
            _trayMenu.MenuItems.Add(_closeMenuItem);

            _trayIcon = new NotifyIcon
                            {
                                Icon = System.Drawing.Icon.ExtractAssociatedIcon(Path + @"\Laura.exe"),
                                Text = "LAura",
                                Visible = true,
                                ContextMenu = _trayMenu
                            };
        }

        void OptionsMenuItemClick(object sender, EventArgs e)
        {
            if (m_optionsWindow != null && m_optionsWindow.IsVisible)
            {
                m_optionsWindow.Activate();
                return;
            }

            m_optionsWindow = new Options();
            m_optionsWindow.OptionsChanged += OptionsWindowOptionsChanged;

            m_optionsWindow.ShowDialog();
        }

        void OptionsWindowOptionsChanged(object sender, EventArgs e)
        {
 
        }

        void CloseMenuItemClick(object sender, EventArgs e)
        {
            System.Drawing.Color c = System.Drawing.Color.FromArgb(000, 000, 000);
            SetDwmColor(c);
            _trayIcon.Visible = false;
            _trayIcon.Dispose();
            this.Close();
        }
        
        void TimerTick(object sender, EventArgs e)
        {
            if (m_errorLync == 1)
            {
                m_localLync = new Lync();
                m_errorLync = m_localLync.LoadLync();
            } 
            else
            {
                SetLyncStatus(m_localLync);
            }
        }

        void SetLyncStatus(Lync localLync)
        {
            m_red = 0;
            m_green = 0;
            m_blue = 0;

            m_lyncActualStatus = localLync.GetLyncStatus();
            if (m_lyncActualStatus != "error")
            {
                if (m_lyncLastStatus != m_lyncActualStatus)
                {
                    switch (m_lyncActualStatus)
                    {
                        case "Free":
                            m_red = 10;
                            m_green = 250;
                            m_blue = 52;
                            break;
                        case "Busy":
                            m_red = 255;
                            m_green = 4;
                            m_blue = 4;
                            break;
                        case "Away":
                            m_red = 249;
                            m_green = 255;
                            m_blue = 4;
                            break;
                        case "TemporarilyAway":
                            m_red = 249;
                            m_green = 255;
                            m_blue = 4;
                            break;
                        default:
                            m_red = 0;
                            m_green = 0;
                            m_blue = 0;
                            break;
                    }

                    System.Drawing.Color c = System.Drawing.Color.FromArgb(m_red, m_green, m_blue);
                    SetDwmColor(c);
                    m_lyncLastStatus = m_lyncActualStatus;
                    m_lyncActualStatus = null;
                }
            }
            else
            {
                System.Drawing.Color c = System.Drawing.Color.FromArgb(000, 000, 000);
                SetDwmColor(c);
                m_errorLync = 1;
            }
        }

        private static void SetDwmColor(System.Drawing.Color newColor)
        {
            if (WinAPI.DwmIsCompositionEnabled())
            {
                WinAPI.DWM_COLORIZATION_PARAMS color;
                WinAPI.DwmGetColorizationParameters(out color);

                color.Color1 = System.Drawing.Color.FromArgb(255, newColor.R, newColor.G, newColor.B).ToArgb();
                color.Intensity = 55;
                WinAPI.DwmSetColorizationParameters(ref color, 0);
            }
        }

        private void ColorAnimationCurrentTimeInvalidated(object sender, EventArgs e)
        {
            txt.Text = Bg.Color.R + "|" + Bg.Color.G + "|" + Bg.Color.B;
            System.Drawing.Color c = System.Drawing.Color.FromArgb(/*((SolidColorBrush)Grid.Background).Color.A*/ 255, Bg.Color.R, Bg.Color.G, Bg.Color.B);
            SetDwmColor(c);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        #endregion
    }
}
